package cn.cjlmonster.demo02.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.cjlmonster.demo02.R;

/**
 * This project is Demo02
 * Created by cjl on 2021/11/6 1:50 上午.
 */

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.Holder> {

    private static final String TAG = "测试数据";

    private final LayoutInflater inflater;
    private List<Integer> mColors;

    public CardAdapter(Context context, List<Integer> mColors) {
        this.inflater = LayoutInflater.from(context);
        this.mColors = mColors;
    }

    public void resetData(List<Integer> data) {
        this.mColors = data;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_card, parent, false);
        Holder holder = new Holder(view);
        Log.i(TAG, "onCreateViewHolder: " + holder);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        Log.i(TAG, "onBindViewHolder: " + holder);
           holder.layout_card.setCardBackgroundColor(mColors.get(position));
    }

    @Override
    public int getItemCount() {
        return mColors.size();
    }

    static class Holder extends RecyclerView.ViewHolder {

        CardView layout_card;
        TextView tv_msg;

        public Holder(@NonNull View itemView) {
            super(itemView);
            setIsRecyclable(false);
            layout_card = itemView.findViewById(R.id.layout_card);
            tv_msg = itemView.findViewById(R.id.tv_msg);
        }
    }
}
